import {ClassicModel} from '../../models/classic'
import {LikeModel} from '../../models/like'

let classicModel = new ClassicModel();
let likeModel = new LikeModel();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        classicData: null,
        latest: true,
        first: false,
        likeCount: 0,
        likeStatus: false
    },

    onLike(event) {
        console.log(event);
        let behavior = event.detail.behavior;
        likeModel.like(behavior, this.data.classicData.id, this.data.classicData.type);
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // 数据更新 storage
        classicModel.getLatest()
            .then(res => {
                const { like_status, fav_nums } = res;
                this.setData({
                    classicData: res,
                    likeStatus: like_status,
                    likeCount: fav_nums
                })
                // latestClassic latestIndex currentClassic currentIndex
            })
    },

    onNext() {
        this._upDateClassic('next');
    },

    onPrevious() {
        this._upDateClassic('previous');
    },

    _upDateClassic(nextOrPrevious) {
        const index = this.data.classicData.index;
        classicModel.getClassic(index, nextOrPrevious)
            .then(res => {
                console.log("有基友拉我裤链", res);
                const { id, type } = res;
                this._getLikeStatus(id, type);
                this.setData({
                    classicData: res,
                    latest: classicModel.isLatest(res.index),
                    first: classicModel.isFirst(res.index)
                })
            })
    },
    _getLikeStatus(artID, category) {
        likeModel.getClassicLikeStatus(artID, category)
            .then(res => {
                console.log('likeModule->like', res);
                const { like_status, fav_nums } = res;
                this.setData({
                    likeStatus: like_status,
                    likeCount: fav_nums
                })
            })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})